package com.controller;

import com.entity.Course;
import com.github.pagehelper.PageInfo;
import com.service.CourseSrevice;
import com.util.AjaxResult;
import com.vo.CourseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("/course")

public class CourseCtl {
    @Autowired
    CourseSrevice courseSrevice;

    //李奇州
    @RequestMapping("/findAllCourse.do")
    @ResponseBody
    public PageInfo<Course> findAllCourse(Integer pageNo, Integer course_type_id,HttpSession session){
        return courseSrevice.selectAllCourseByTypeID(pageNo,course_type_id,session);
    }

    //@李奇洲
    @RequestMapping("/loadRecCourse.do")
    @ResponseBody
    //@PathVariable(value = "course_id",required = false)
    public AjaxResult loadRecCourse (Integer course_id){
        System.out.println("loadRecCourse方法执行了 course_id="+course_id);
        List<Course> top3Courses = courseSrevice.findTop3CoursesByCourseId(1);
        System.out.println("测试"+top3Courses);
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setObj(top3Courses);
        return ajaxResult;
    }

    //  吴彬彬  条件查询
    @RequestMapping("/findCourse.do")
    @ResponseBody
    public AjaxResult findCourse(CourseVo courseVo) {

        AjaxResult result = new AjaxResult(true, "查询成功", null);

        try {
            PageInfo<CourseVo> info = courseSrevice.findCourse(courseVo);
            result.setObj(info);
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMsg(e.getMessage());
        }

        return result;
    }

    //吴彬彬  修改状态
    @RequestMapping("/toggle.do")
    public void toggle(HttpServletRequest request, HttpServletResponse response) {
        Integer id = Integer.valueOf(request.getParameter("id"));
        Integer status = Integer.valueOf(request.getParameter("status"));
        courseSrevice.toggle(id, status);
    }

    @RequestMapping("/show.do")
    @ResponseBody
    public AjaxResult show(Integer id) {

        AjaxResult result = null;

        try {
            Course show = courseSrevice.show(id);

            result = new AjaxResult(true, "成功", show);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return result;

    }

    //吴彬彬  增加
    @RequestMapping("/addCourse.do")
    public AjaxResult addCourse(@RequestParam("multiFiles") MultipartFile multiFiles, Course course, HttpSession session) throws IOException {

        String path = session.getServletContext().getRealPath("/upload/course_cover");
        File file = new File(path);
        file.mkdir();
        String fileName = multiFiles.getOriginalFilename();
        multiFiles.transferTo(new File(file, fileName));
        course.setCover_image_url(fileName);
        int i = courseSrevice.addCourse(course);
        AjaxResult result = null;
        if (i > 0) {
            result = new AjaxResult(true, "成功", null);
        } else {
            result = new AjaxResult(true, "失败", null);
        }

        System.out.println("======="+ course.getCover_image_url());


        return result;
    }

    //吴彬彬  修改
    @RequestMapping("modifyCourse.do")

    public AjaxResult modifyCourse(@RequestParam("multiFiles") MultipartFile multiFiles, Course course, HttpSession session) throws IOException {
//        Course course1 = getCourse(multiFiles, course, session);
        String path = session.getServletContext().getRealPath("/upload/course_cover");
        File file = new File(path);
        file.mkdir();
        String fileName = multiFiles.getOriginalFilename();
        multiFiles.transferTo(new File(file, fileName));
        course.setCover_image_url(fileName);

        courseSrevice.modifyCourse(course);

        AjaxResult result = new AjaxResult(true,"成功",null);

        return result;
    }


    //杨跃森
    @RequestMapping("/showTop15.do")
    @ResponseBody
    public List<Course> findTop15Course() {
        List<Course> top15 = courseSrevice.findTop15();
        return top15;

    }

    @RequestMapping("/findAllCourseForFront.do")
    public String findAllCourseForFront(){

        return "/front/front_mycourse";
    }

    @RequestMapping("/idHolder.do")
    public String jumpToChapter(Integer id, HttpSession session) {
        session.setAttribute("id", id);
        return "background/back_courseReourceSet";
    }
    

}
